package com.example.onedemotest.slice;

import com.example.onedemotest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

public class ListSliceDemo extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(createComponentContainer());
    }

    private ArrayList<String> createLocalData(){
        ArrayList<String> result=new ArrayList<>();
        for (int i=0;i<50;i++){
            result.add("hhahah"+i);
        }
        return result;
    }

    private ComponentContainer createComponentContainer(){
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_list_slice, null, false);
        ListContainer listContainer= (ListContainer) layout.findComponentById(ResourceTable.Id_listSlice);
        ListProvider listProvider = new ListProvider(createLocalData(), this);
        listContainer.setItemProvider(listProvider);
        listContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                listProvider.notifyDataChanged();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
                String item = (String) listContainer.getItemProvider().getItem(position);
                new ToastDialog(ListSliceDemo.this).setText(item).setAlignment(LayoutAlignment.CENTER).show();
            }
        });
        return layout;
    }
}
